<template>
  <div>
    <el-menu
      :default-active="activeIndex"
      class="el-menu-demo"
      mode="horizontal"
      @select="handleSelect"
      background-color="#545c64"
      text-color="#fff"
      active-text-color="#ffd04b"
    >
      <el-menu-item index="1">首页</el-menu-item>
      <el-menu-item index="2">博客</el-menu-item>
      <el-menu-item index="3">视频教程</el-menu-item>
      <el-menu-item
        index="4"
        v-if="!(userInfo && userInfo.id)"
        @click="handleToLogs"
        >登录&nbsp;/&nbsp;注册</el-menu-item
      >
      <el-menu-item index="5" v-if="userInfo && userInfo.id">
        <img
          :src="userInfo.avatar?userInfo.avatar:'https://gulimall-jia.oss-cn-beijing.aliyuncs.com/avatar01.jpg'"
          alt="头像"
          style="width: 40px; height: 40px; border-radius: 50%"
        />
        <span style="margin: 0 10px">{{ userInfo.username }}</span>
      </el-menu-item>
      <el-menu-item
        index="6"
        v-if="userInfo && userInfo.id"
        @click="handleLogout"
        >退 出</el-menu-item
      >
    </el-menu>
  </div>
</template>

<script>
export default {
  name: "navbar",
  data() {
    return {
      activeIndex: "1",
      userInfo: {
        id: "",
      },
    };
  },
  created() {
    this.userInfo = this.$store.getters.getUser;
  },
  methods: {
    handleSelect(key, keyPath) {
      this.activeIndex = key;
      this.$emit("bar-change",this.activeIndex)
    },
    handleToLogs() {
      this.$router.push("/logs");
    },
    handleLogout() {
      this.$store.commit("REMOVE_INFO");
      this.$router.go(0);
    },
  },
};
</script>

<style lang="scss" scoped>
.el-menu {
  box-sizing: border-box;
  padding: 0 200px;
  position: relative;
}
.el-menu-item:last-child {
  float: right;
}
</style>